package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import util.ImageUtil;
import util.KMeans;

@SuppressWarnings("serial")
public class ToolbarStandard extends JToolBar {
	
	JButton bt1;
	JButton bt2;
	JButton bt3;
	JButton bt4;
	JButton bt5;
	JButton bt6;
	JButton bt7;
	JButton bt8;
	JButton bt9;
	JButton bt10;
	JButton bt11;
	JButton bt12;
	JButton bt13;
	JButton bt14;
	JButton bt15;
	JButton bti;
	JButton btc;
	JLabel jl;
	static BufferedImage img;
	static KMeans pozadina = null;
	static BufferedImage originalImage;
	static Point startingPoint;
	static Point endingPoint;
	static BufferedImage reloadImage;
	
    public static short[][] kMeansRecognition(BufferedImage bmp)
    {
        // -------- primeniti filter na sliku ----------------
        short[][][] slika = ImageUtil.bitmapToColorMatrix(bmp);
        int w = slika[0].length;
        int h = slika.length;
        short[][] nslika = new short[h][w];

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
          	  short cbR = slika[y][x][0];
                short cbG = slika[y][x][1];
                short cbB = slika[y][x][2];

                Color cc = new Color(cbR, cbG, cbB);

                // za svaki piksel na slici odrediti kojem klasteru pripada
                // pronalazenje indeksa (rednog broja) klastera kojem pripada piksel
                int najbliza = 0;
                    if (pozadina.grupe.get(0).rastojanje(cc) < pozadina.grupe.get(1).rastojanje(cc))
                        najbliza = 0;
                    else
                  	  najbliza = 1;
                int max = 0;
                if (pozadina.grupe.get(0).elementi.size() < pozadina.grupe.get(1).elementi.size())
                    max = 1;
                if (najbliza == 0)
                    nslika[y][x] = 255;
                else
                    nslika[y][x] = 0;
            }
        }
       return nslika;
    }
	
	public ToolbarStandard() {
	super(SwingConstants.HORIZONTAL);
	
		bt1 = new JButton("Ucitaj Sliku");
		
		
		bt1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser fc = new JFileChooser();
		        int res = fc.showOpenDialog(null);
		        try {
		            if (res == JFileChooser.APPROVE_OPTION) {
		                File file = fc.getSelectedFile();
		                MainFrame.getInstance().setImage(file);
		                try{
		            		img = ImageIO.read(file);
		            		reloadImage = ImageIO.read(file);
		            		}catch(Exception ex){
		            		}
		               
		                originalImage=img;
			               MainFrame.getInstance().srednji.setImg(img);
			               BufferedImage nova = new BufferedImage(
			       				img.getWidth(),
			    				img.getHeight(),
			    				img.getType());
			               MainFrame.getInstance().srednji.setScreenCopy(nova);
			              
			              
			               MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
			               //MainFrame.getInstance().srednji.revalidate();
			               MainFrame.getInstance().srednji.repaint();
			              // MainFrame.getInstance().srednji.getScreenLabel().revalidate();
			               //MainFrame.getInstance().srednji.getScreenLabel().repaint();
			              
			              // MainFrame.getInstance().srednji.getScreenLabel().setSize(img.getWidth(), img.getHeight());
		                
		            } // Oops!
		            else {
		                JOptionPane.showMessageDialog(null,
		                        "You must select one image to be the reference.", "Aborting...",
		                        JOptionPane.WARNING_MESSAGE);
		            }
		        } catch (Exception iOException) {
		        }
				
			}
		});
		add(bt1);
		
		addSeparator(new Dimension(15,3));
		
		bt2 = new JButton("Clear");
		
		
		bt2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				
			}
		});
		add(bt2);
		
		addSeparator(new Dimension(15,3));
		
		bt3 = new JButton("Reload");
		
		
		bt3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				MainFrame.getInstance().srednji.setImg(reloadImage);
				img=reloadImage;
				
	            BufferedImage nova = new BufferedImage(
	            		reloadImage.getWidth(),
	            		reloadImage.getHeight(),
	            		reloadImage.getType());
	            MainFrame.getInstance().srednji.setScreenCopy(nova);
	            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
	               MainFrame.getInstance().srednji.repaint(reloadImage,nova);
	               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				
			}
		});
		add(bt3);
		
		addSeparator(new Dimension(15,3));
		
		bt4 = new JButton("grey");
		
		
		bt4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt4);
		
		addSeparator(new Dimension(15,3));
		
		btc = new JButton("crop");
		
		
		btc.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				MainFrame.getInstance().srednji.setImg(img);
				originalImage=img;
	            BufferedImage nova = new BufferedImage(
	            		img.getWidth(),
	            		img.getHeight(),
	            		img.getType());
	            MainFrame.getInstance().srednji.setScreenCopy(nova);
	            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
	               MainFrame.getInstance().srednji.repaint(img,nova);
	               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				
				
				
			}
		});
		add(btc);
		
		addSeparator(new Dimension(15,3));
		
		bt5 = new JButton("binary");
		
		
		bt5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.matrixToBinaryTiles(slika, 10, 10);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}
				else{
					 img = ImageUtil.matrixToBitmap(slika2);
			            MainFrame.getInstance().srednji.setImg(img);
			            BufferedImage nova = new BufferedImage(
			            		img.getWidth(),
			            		img.getHeight(),
			            		img.getType());
			            MainFrame.getInstance().srednji.setScreenCopy(nova);
			            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
			               MainFrame.getInstance().srednji.repaint(img,nova);
			               MainFrame.getInstance().srednji.getScreenLabel().repaint();
	               
	               
				}
				
				
			}
		});
		add(bt5);
		
		addSeparator(new Dimension(15,3));
		
		bt6 = new JButton("erosion");
		
		
		bt6.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.erosion(slika);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt6);
		
		addSeparator(new Dimension(15,3));
		
		bt7 = new JButton("dilation");
		
		
		bt7.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.dilation(slika);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt7);
		
		addSeparator(new Dimension(15,3));
		
		
	bt8 = new JButton("blur");
		
		
		bt8.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.blur(slika);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt8);
		
		addSeparator(new Dimension(15,3));
		
	bt9 = new JButton("invert");
		
		
		bt9.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.invert(slika);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt9);
		
		addSeparator(new Dimension(15,3));
		
	bt10 = new JButton("ivice");
		
		
		bt10.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int x=0;
				int y=0;
				short[][] slika = ImageUtil.bitmapToByteMatrix(img);
				short[][] slika2 = ImageUtil.bitmapToByteMatrix(originalImage);
				slika = ImageUtil.iviceSobel(slika);
				if(slika2.length != slika.length && slika2[0].length != slika[0].length){
					System.out.println(slika.length +"\t"+slika[0].length+"\t");
					System.out.println(endingPoint.y-startingPoint.y);
					System.out.println(endingPoint.x-startingPoint.x);
				//(endingPoint.y-startingPoint.y)+"\t"+endingPoint.x-startingPoint.x);
					for(int i=startingPoint.y;i<endingPoint.y;i++){
						for(int j=startingPoint.x; j<endingPoint.x; j++){
							slika2[i][j]=slika[y][x];
							Color c = new Color(slika[y][x], slika[y][x], slika[y][x]);
			            	originalImage.setRGB(j, i, c.getRGB());
							x++;
							
						}
					x=0;	
					y++;
					}
					img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(originalImage);
		            BufferedImage nova = new BufferedImage(
		            		originalImage.getWidth(),
		            		originalImage.getHeight(),
		            		originalImage.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(originalImage,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
					
				}else{
		            img = ImageUtil.matrixToBitmap(slika2);
		            MainFrame.getInstance().srednji.setImg(img);
		            BufferedImage nova = new BufferedImage(
		            		img.getWidth(),
		            		img.getHeight(),
		            		img.getType());
		            MainFrame.getInstance().srednji.setScreenCopy(nova);
		            MainFrame.getInstance().srednji.getScreenLabel().setIcon(new ImageIcon(nova));
		               MainFrame.getInstance().srednji.repaint(img,nova);
		               MainFrame.getInstance().srednji.getScreenLabel().repaint();
				}
				
				
			}
		});
		add(bt10);
		
		addSeparator(new Dimension(15,3));
			
		
		
	
	
	bt11 = new JButton("Kmeans");
	
	
	bt11.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
              short[][][] slika = ImageUtil.bitmapToColorMatrix(img);
          	int h = slika.length;
            int w = slika[0].length;
              HashMap<Color, Integer> colorHistogram = new HashMap<Color, Integer>();
              for (int y = 0; y < h; y++)
              {
                  for (int x = 0; x < w; x++)
                  {
                      short cbR = slika[y][x][0];
                      short cbG = slika[y][x][1];
                      short cbB = slika[y][x][2];

                      Color cc = new Color(cbR, cbG, cbB);
                      if (colorHistogram.containsKey(cc))
                      {
                          int n = colorHistogram.get(cc);
                          n++;
                          colorHistogram.put(cc, n);
                      }
                      else
                      {
                          colorHistogram.put(cc, 1);
                      }
                  }
              }
      		
              pozadina = new KMeans(); // inicijalizacija
              int brojGrupa = 2;
              // inicijalizovati podatke koje ce K-means da klasterizuje i pokrenuti algoritam
              // postavljanje elemenata koje je potrebno klasterizovati
              for (Color key : colorHistogram.keySet())
              {
                  pozadina.elementi.add(key);
              }
                 
              // pokretanje K-means algoritma
              pozadina.podeliUGRupe(brojGrupa, 10);
          	short[][] nslika = new short[h][w]; 
              
              for (int y = 0; y < h; y++)
              {
                  for (int x = 0; x < w; x++)
                  {
                	  short cbR = slika[y][x][0];
                      short cbG = slika[y][x][1];
                      short cbB = slika[y][x][2];

                      Color cc = new Color(cbR, cbG, cbB);

                      // za svaki piksel na slici odrediti kojem klasteru pripada
                      // pronalazenje indeksa (rednog broja) klastera kojem pripada piksel
                      int najbliza = 0;
                          if (pozadina.grupe.get(0).rastojanje(cc) < pozadina.grupe.get(1).rastojanje(cc))
                              najbliza = 0;
                          else
                        	  najbliza = 1;
                      int max = 0;
                      if (pozadina.grupe.get(0).elementi.size() < pozadina.grupe.get(1).elementi.size())
                          max = 1;
                      if (najbliza == 0)
                          nslika[y][x] = 255;
                      else
                          nslika[y][x] = 0;
                  }
              }
              img = ImageUtil.matrixToBitmap(nslika);
            MainFrame.getInstance().srednji.setImg(img);
               MainFrame.getInstance().srednji.repaint();
			
			
		}
	});
	add(bt11);
	
	addSeparator(new Dimension(15,3));
		
}
	
}
	
	

